﻿#region Using statements
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SmartFrequency.Utils;
using SmartFrequency.SoundCapture;
#endregion

namespace SmartFrequency.GUI
{
    public partial class VoiceRecorder : Form
    {
        #region Private variables
        private SoundCapturePlay SoundObject;

        enum OpState
        {
            STOPING = 0,
            PLAYING,
            RECORDING,
            PAUSING
        };

        private OpState OperationState;

        // Used to calculate eplassed time during recording or playback (VoiceRecordingPlayback)
        private DateTime Timer_VRP;
        private string PlayTime, RecordTime, OpenPictureFileName;

        private Language.Language_e PLanguage = Language.Language_e.TR;
        private Language ProgramLanguage;
        private Settings PSettings;
        private TimeSpan ElapsedRecord, ElapsedPlay, ElapsedPause;
        private Utilities PUtils = new Utilities();
        #endregion

        #region Private functions
        private void SetState(OpState value) { OperationState = value; }
        private OpState GetState() { return OperationState; }
        #endregion

        #region Public funcitons
        public VoiceRecorder()
        {
            InitializeComponent();

            this.Text = PUtils.Version;
            PSettings = new Settings("settings.txt");
            PSettings.Load();

            switch (PSettings.GetMember("language"))
            {
                default:
                case "TR":
                case "tr":
                    PLanguage = Language.Language_e.TR;
                    label6.Left += 20;
                    break;
                case "ENG":
                case "eng":
                    PLanguage = Language.Language_e.ENG;
                    break;
            }
            ProgramLanguage = new Language(PLanguage);

            TimeSpan tm1 = TimeSpan.FromMilliseconds(Convert.ToUInt32(PSettings.GetMember("duration")) * 1000);
            RecordTime = string.Format("{0:D2}:{1:D2}", (int)tm1.TotalMinutes, tm1.Seconds);

            pictureBox3.Image = ımageList1.Images[1];
            pictureBox4.Image = ımageList1.Images[2];
            pictureBox5.Image = ımageList1.Images[0];

            // tab page 2 language changes
            label6.Text = ProgramLanguage.FrmMsgToString(Language.FormMsg_e.FORM1_HEAD);
            label7.Text = ProgramLanguage.FrmMsgToString(Language.FormMsg_e.FORM1_LABEL1);
            label8.Text = ProgramLanguage.FrmMsgToString(Language.FormMsg_e.FORM1_LABEL2);
            menuItem2.Text = ProgramLanguage.FrmMsgToString(Language.FormMsg_e.MENU7_TEXT);


            if (SoundObject != null)
                SoundObject.RecordDestroy();

            SoundObject = new SoundCapturePlay();
            SoundObject.SetLanguage(PLanguage);
            SoundObject.RecordParams.SamplingRate = Convert.ToInt32(PSettings.GetMember("samplingrate"));       //22050;
            SoundObject.RecordParams.ChannelNo = Convert.ToInt32(PSettings.GetMember("channelno"));             //1;
            SoundObject.RecordParams.BitsPerSample = Convert.ToInt32(PSettings.GetMember("bitspersample"));     //16;
            SoundObject.RecordParams.Duration = Convert.ToInt32(PSettings.GetMember("duration")) * 1000;        //300000;   // 300 sec
            SoundObject.RecordParams.BufferSize = Convert.ToInt32(PSettings.GetMember("recordbuffer"));         //16 * 22050;

            if (SoundObject.RecordInit() != 1)
                return;

            SoundObject.PlayParams.BufferSize = Convert.ToInt32(PSettings.GetMember("playbuffer"));           //16 * 22050;
            SoundObject.PlayParams.LeftVolume = (ushort)0xFFFF;
            SoundObject.PlayParams.RightVolume = (ushort)0xFFFF;
            if (SoundObject.PlayInit() != 1)
                return;
        }

        private void VoiceSettingsPicture_Click(object sender, EventArgs e)
        {
            RecordSettings RecordingSettingsPage = new RecordSettings();

            RecordingSettingsPage.Show();
        }

        private void OpenPicture_Click(object sender, EventArgs e)
        {
            /*
            openFileDialog1.Filter = "Wave files (*.wav)|*.wav|All files (*.*)|*.*";
            //openFileDialog1.InitialDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
            openFileDialog1.InitialDirectory = "\\My Documents\\My Music\\";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                OpenPictureFileName = openFileDialog1.FileName;
                SoundObject.PlayFileName(OpenPictureFileName);
            }
            */

            OpenFile OpenFilePage = new OpenFile();
            //OpenFilePage.Show();
            OpenFilePage.ShowDialog();
            OpenPictureFileName = OpenFilePage.soundFile;
            OpenFilePage.Dispose();
            label10.ForeColor = Color.Black;
            SoundObject.PlayFileName(OpenPictureFileName);
        }

        private void StopPicture_Click(object sender, EventArgs e)
        {
            label10.ForeColor = Color.Black;
            switch (GetState())
            {
                case OpState.RECORDING:
                    if (SoundObject.RecordStop() != -1)
                    {
                        timer2.Enabled = false;
                        pictureBox3.Image = ımageList1.Images[1];
                        pictureBox4.Image = ımageList1.Images[2];
                        SetState(OpState.STOPING);
                    }
                    else
                    {
                        MessageBox.Show(ProgramLanguage.ErrMsgToString(Language.FailureMsg_e.ERR_STOPRECORDING));
                    }
                    break;

                case OpState.PAUSING:
                case OpState.PLAYING:
                    if (SoundObject.PlayStop() != -1)
                    {
                        timer2.Enabled = false;
                        pictureBox3.Image = ımageList1.Images[1];
                        pictureBox4.Image = ımageList1.Images[2];
                        label10.Text = "00:00" + " / " + PlayTime;
                        SetState(OpState.STOPING);
                    }
                    else
                    {
                        MessageBox.Show(ProgramLanguage.ErrMsgToString(Language.FailureMsg_e.ERR_STOPPLAYING));
                    }
                    break;

                default:
                    break;
            }
        }

        private void PlayPicture_Click(object sender, EventArgs e)
        {
            label10.ForeColor = Color.Black;
            switch (GetState())
            {
                case OpState.STOPING:
                    if (SoundObject.PlayStart() != -1)
                    {

                        timer2.Enabled = true;
                        Timer_VRP = DateTime.Now;

                        ElapsedPause = TimeSpan.Zero;
                        TimeSpan tm = TimeSpan.FromMilliseconds(SoundObject.PlayTime());
                        PlayTime = string.Format("{0:D2}:{1:D2}", (int)tm.TotalMinutes, tm.Seconds);
                        pictureBox3.Image = ımageList1.Images[7];
                        pictureBox4.Image = ımageList1.Images[4];
                        SetState(OpState.PLAYING);
                    }
                    else
                    {
                        MessageBox.Show(ProgramLanguage.ErrMsgToString(Language.FailureMsg_e.ERR_PLAY));
                        label9.Text = "";
                    }

                    break;

                case OpState.PLAYING:
                    if (SoundObject.PlayPause() != -1)
                    {
                        timer2.Enabled = false;
                        pictureBox3.Image = ımageList1.Images[1];
                        pictureBox4.Image = ımageList1.Images[2];
                        ElapsedPause = ElapsedPlay;

                        SetState(OpState.PAUSING);
                    }
                    else
                    {
                        MessageBox.Show(ProgramLanguage.ErrMsgToString(Language.FailureMsg_e.ERR_STOPPLAYING));
                    }
                    break;

                case OpState.PAUSING:
                    if (SoundObject.PlayResume() != -1)
                    {
                        timer2.Enabled = true;

                        //ElapsedPlay = DateTime.Now - Timer_VRP
                        Timer_VRP = DateTime.Now;
                        pictureBox3.Image = ımageList1.Images[1];
                        pictureBox4.Image = ımageList1.Images[4];
                        SetState(OpState.PLAYING);

                    }
                    break;

                default:
                    break;
            }
        }

        private void RecordPicture_Click(object sender, EventArgs e)
        {
            label10.ForeColor = Color.Black;
            switch (GetState())
            {
                case OpState.STOPING:
                    string FileName = SoundObject.RecordFileName();
                    if (SoundObject.RecordStart() != -1)
                    {
                        int i = FileName.LastIndexOf("\\") + 1;
                        label9.Text = FileName.Substring(i, (FileName.Length - i));

                        timer2.Enabled = true;
                        Timer_VRP = DateTime.Now;

                        pictureBox3.Image = ımageList1.Images[3];
                        pictureBox4.Image = ımageList1.Images[8];
                        SetState(OpState.RECORDING);
                    }
                    else
                    {
                        MessageBox.Show(ProgramLanguage.ErrMsgToString(Language.FailureMsg_e.ERR_RECORD));
                        label9.Text = "";
                    }
                    break;

                case OpState.RECORDING:
                    if (SoundObject.RecordStop() != -1)
                    {
                        timer2.Enabled = false;
                        pictureBox3.Image = ımageList1.Images[1];
                        pictureBox4.Image = ımageList1.Images[2];
                        SetState(OpState.STOPING);
                    }
                    else
                    {
                        MessageBox.Show(ProgramLanguage.ErrMsgToString(Language.FailureMsg_e.ERR_STOPRECORDING));
                    }
                    break;

                default:
                    break;
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            switch (GetState())
            {
                case OpState.RECORDING:
                    if (SoundObject.RecordStatus() == true)
                    {
                        StopPicture_Click(sender, e);
                    }
                    else
                    {
                        ElapsedRecord = DateTime.Now - Timer_VRP;
                        //label10.Text = ((int)ElapsedRecord.TotalMinutes).ToString() + ":" + ElapsedRecord.Seconds.ToString() + " / " + RecordTime;
                        label10.Text = string.Format("{0:D2}:{1:D2}", (int)ElapsedRecord.TotalMinutes, ElapsedRecord.Seconds) + " / " + RecordTime;
                    }
                    break;
                case OpState.PLAYING:
                    if (SoundObject.PlayStatus() == true)
                    {
                        StopPicture_Click(sender, e);
                    }
                    else
                    {
                        ElapsedPlay = DateTime.Now - Timer_VRP + ElapsedPause;
                        //label10.Text = ((int)ElapsedPlay.TotalMinutes).ToString() + ":" + ElapsedPlay.Seconds.ToString() + " / " + PlayTime;
                        //label10.Text = string.Format("{0: mm:ss}", ElapsedPlay) + " / " + PlayTime;
                        label10.Text = string.Format("{0:D2}:{1:D2}", (int)ElapsedPlay.TotalMinutes, ElapsedPlay.Seconds) + " / " + PlayTime;
                    }

                    break;
                default:
                    break;
            }
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            SoundObject.Dispose();
            Dispose();
        }

        private void VoiceRecorder_Paint(object sender, PaintEventArgs e)
        {
            TimeSpan tm1 = TimeSpan.FromMilliseconds(Convert.ToUInt32(PSettings.GetMember("duration")) * 1000);
            RecordTime = string.Format("{0:D2}:{1:D2}", (int)tm1.TotalMinutes, tm1.Seconds);

            if (OpenPictureFileName == null)
            {
                label9.Text = "";
                //label10.Text = "";
                label10.Text = "00:00" + " / " + RecordTime;
                label10.ForeColor = Color.Gray;
            }
            else
            {
                SoundObject.PlayFileName(OpenPictureFileName);
                int i = OpenPictureFileName.LastIndexOf("\\") + 1;
                string temp = OpenPictureFileName.Substring(i, OpenPictureFileName.Length - i);
                label9.Text = temp;

                TimeSpan tm2 = TimeSpan.FromMilliseconds(SoundObject.PlayFileLength());
                label10.Text = "00:00 / " + string.Format("{0:D2}:{1:D2}", (int)tm2.TotalMinutes, tm2.Seconds);
            }
        }
        #endregion
    }
}